package org.goobs.internet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;

public class Email {
	private static int numEmails = 0;
	
	private Properties props = new Properties();
	private HashSet <String> attachments = new HashSet <String> ();
	
	public Email(){	numEmails++; }
	public Email(String to){
		numEmails++;
		String[] tA = {to};
		props.put("to", tA);
	}
	public Email(String[] to){
		numEmails++;
		props.put("to", to);
	}
	public Email(String[] to, String from){
		numEmails++;
		props.put("to", to);
		props.put("from", from);
	}
	public Email(String[] to, String from, String subject){
		numEmails++;
		props.put("to", to);
		props.put("from", from);
		props.put("subject", subject);
	}
	public Email(String[] to, String from, String subject, String replyTo){
		numEmails++;
		props.put("to", to);
		props.put("from", from);
		props.put("subject", subject);
		props.put("reply-To", replyTo);
	}
	public Email(String[] to, String from, String subject, String replyTo, String message){
		numEmails++;
		props.put("to", to);
		props.put("from", from);
		props.put("subject", subject);
		props.put("reply-To", replyTo);
		props.put("message", message);
	}
	
	public void setTo(String to){ String[] tA={to}; props.put("to", tA); }
	public void setTo(String[] to){ props.put("to", to); }
	public void setFrom(String from){ props.put("from", from); }
	public void setSubject(String subject){ props.put("subject", subject); }
	public void setReplyTo(String replyTo){ props.put("replyTo", replyTo); }
	public void setText(String message){ props.put("message", message); }
	public void setHtml(String html){ props.put("html", html); }
	public void setDate(Date date){ props.put("date", date); }
	
	public boolean attach(String filename){
		File f = new File(filename);
		if(f.exists()){
			attachments.add(filename);
			return true;
		}else{
			return false;
		}
	}
	public boolean detach(String filename){
		return attachments.remove(filename);
	}
	
	public boolean setMessageFromFile(String filename, boolean HTML){
		try {
			File f = new File(filename);
			FileInputStream input = new FileInputStream(f);
			byte[] buffer = new byte[input.available()];
			input.read(buffer);
			String s = new String(buffer);
			if(HTML){
				setHtml(s);
			}else{
				setText(s);
			}
			return true;
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
	}
	
	public void fillTemplate(HashMap <String, String> rpl){
		fillProp("subject", rpl);
		fillProp("message", rpl);
		fillProp("html", rpl);
	}
	
	private void fillProp(String prop, HashMap <String, String> rpl){
		String msg = props.getProperty(prop);
		if(msg == null){
			return;
		}
		Iterator <String> keys = rpl.keySet().iterator();
		while(keys.hasNext()){
			String key = keys.next();
			msg = msg.replaceAll(key, rpl.get(key));
		}
		props.setProperty(prop, msg);
	}
	
	public Email clone(){
		Email rtn = new Email();
		Iterator <Object> iter = props.keySet().iterator();
		while(iter.hasNext()){
			Object key = iter.next();
			rtn.props.put(key, props.get(key));
		}
		return rtn;
	}
	
	public String toString(){
		return "[email " + numEmails + " '" + props.getProperty("subject") + "']";
	}
	
	protected Properties getProperties(){
		return props;
	}
	protected HashSet <String> getAttachments(){
		return attachments;
	}
	
}
